package com.flw.eslab.action.admin;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Result;


import com.flw.eslab.model.Resource;
import com.flw.eslab.service.ResourceManager;
import com.opensymphony.xwork2.ActionSupport;
import com.sina.sae.storage.SaeStorage;
import com.sina.sae.util.SaeUserInfo;

@Result(type = "stream",params={"contentDisposition","attachment;filename=${fileRealName}"})
public class DownloadAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5677540922663900748L;
	private ResourceManager resourceManager;
	private String id = "";
	private InputStream inputStream;
	private String fileRealName;

	
	

	public String execute() {
		Resource resource = resourceManager.check(id);
		String temp;
		if (resource == null) {

			temp = "toLogin";

		} else {

			temp = "success";
		}

		return temp;

	}

	/*------------------------------------------------*/
	protected InputStream downloadByStorage(String domainname, String fileName,String realname) {
		SaeStorage saeStorage = new SaeStorage();
		byte[] bytes = saeStorage.read(domainname, fileName);

		setFileRealName(realname);
		
		return (new ByteArrayInputStream(bytes));
	}

	protected InputStream downloadByTemFS(String domainname, String fileName,
			String realname) throws IOException {
		String url = getResource(domainname, fileName, realname);
		File file = new File(url);

		setFileRealName(realname);
		
		return (new FileInputStream(file));
	}

	protected String getResource(String domainname, String fileName,
			String realname) throws IOException {
		String realPath = SaeUserInfo.getSaeTmpPath() + "/";
		int bit = 8192;
		SaeStorage saeStorage = new SaeStorage();
		byte[] bytes = saeStorage.read(domainname, fileName);

		InputStream fis = new ByteArrayInputStream(bytes);
		FileOutputStream fos = new FileOutputStream(realPath + realname);
		byte[] buffer = new byte[bit];
		int len = 0;
		while ((len = fis.read(buffer)) > 0) {
			fos.write(buffer, 0, len);
		}
		fis.close();
		fos.close();

		return realPath + realname;
	}

	protected String encodeName(String fileName)
			throws UnsupportedEncodingException {
		
		return new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
	}

	/*------------------------------------------------*/
	@javax.annotation.Resource
	public void setResourceManager(ResourceManager resourceManager) {
		this.resourceManager = resourceManager;
	}

	/*------------------------------------------------*/
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	// 下载文件的入口流
	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public String getFileRealName() {
		return fileRealName;
	}

	public void setFileRealName(String fileRealName) {
		this.fileRealName = fileRealName;
	}

}